﻿
<!--#Include File = "CustomTemplate.asp" -->

<%
	Call  PageController.RenderPage()
	Public Function Page_Configure()
		PageController.PageTitle = "Content management"
		PageController.AuthorizedRoles =  Array("writer","publisher")
		pageController.RequiresAuthentication  = True
		PageController.PageCSSFiles = "main.css" 		
	End function
%>
<%Public Function Page_RenderForm()%>
	<H2>Content management</H2>
	

	
	<%Page.Execute%>	
	<Span Class="Caption">Content Edit<HR></Span>
	<a href="javascript:clasp.form.doPostBack('SetVisibility', 'Panel')">Hide/Show Panel!</a>

	<%panel%>
	
	
<%End Function%>

<%  '
	Dim txtTitle
	Dim txtDescription
	Dim txtBody
	Dim txtStartpublish
	Dim txtEndpublish
	Dim cboCategory
	Dim strBody
	Dim chkHot
	Dim chkAllowComment
	
	Dim Panel

	Dim mValidation

	Dim cmdSave	
	
	Dim cmdSaveAddNew
	
	Dim recordID
	
	'For upload images
	Dim cmdFile	
	Dim txtName
	Dim imgViewer
	
		
	Public Function Page_Init()
		recordID = 0  'Init
		
		
		Set  txtTitle	= New_ServerTextBoxEX("txtTitle",20,20)
		Set  txtDescription	= New_ServerTextBoxEX("txtDescription",20,20)
		
		Set  txtStartpublish	= New_ServerDateTextBox("txtStartpublish")
		Set  txtEndpublish	= New_ServerDateTextBox("txtEndpublish")	
		Set  cboCategory = New_ServerDropDown("cboCategory")		
		
		Set cmdSave = New_ServerLinkButton("cmdSave")		
		set cmdSaveAddNew = New_ServerLinkButton("cmdSaveAddNew")	
		Set mValidation = New ServerValidationSummary
		Set panel = New_ServerPanel("Panel",870,1300)
		Set chkHot = New_ServerCheckBox("chkHot")
		Set chkAllowComment =New_ServerCheckBox("chkAllowComment")
		Set txtBody = New CuteEditor
		
		'For upload file
		Set txtName				= New_ServerTextBox("txtName")
		Set imgViewer			= New_ServerImage("imgViewer")
		Set cmdFile 			= New_ServerFileUploader("cmdFile","")
		cmdFile.TempUploadPath	= Server.MapPath("../Uploads/content/")	
		
		
	End Function

	Public Function Page_Controls_Init()		
		cmdSave.Text = "Save"
		cmdSaveAddNew.Text = "Save & Add New"
		
		txtBody.ID = "txtBody"
		
		
		txtBody.EditorBodyStyle = "font:normal 12px arial;"
		txtBody.EditorWysiwygModeCss = "asp.css"
		txtBody.Width = 780
		txtBody.Height = 600
		
		
		txtTitle.Mode = 3
		txtTitle.Rows = 1
		txtTitle.Cols = 60
		txtDescription.Mode=3
		txtDescription.Rows = 5
		txtDescription.Cols = 60
		panel.Mode = 2
		'panel.OverFlow = "auto" 'scroll?
		panel.PanelTemplate = "RenderPanel"
		
		'For upload file
		txtName.Text = "yourfile.jpg"	
		cmdFile.Text = "Upload File"	
		cmdFile.FileFilter = "bmp,jpeg,jpg,gif" 	'optional, set to "" to upload any file
		cmdFile.MaxUploadSize = 1000000				'optional, set to 0 to uplaod any size
	End Function

	Public Function Page_Load()
	
		If Not Page.IsPostBack Then	
			If Request.QueryString("id")<>"" Then
				recordID = Clng(Request.QueryString("id"))					
				 
			End if
			Call LoadRecord()
		Else
			'Response.Write("body: " & Request.Form("txtBody"))
			'txtBody.Text = Request.Form("txtBody")
		End If	
		
	End Function
	
	Public Function Page_LoadViewState()
		recordID = Page.ViewState.GetValue("ID") 'Get from viewstate	
		
	End Function
	Public Function Page_PreRender()
		Page.ViewState.Add "ID",recordID 'Add to viewstate :-)
		
		'For upload file
		If imgViewer.ImageSrc ="" Then
			imgViewer.Control.Visible = False
		Else
			imgViewer.Control.Visible = True
		End If	
	
	End Function
	
	Public Function SaveData()
		Dim sSQL
		Dim rs
		
		Dim mode
		Call Validate()
		If Not mValidation.IsValid Then
			Exit Function
		End If		
		
		
		'For upload file
		If txtName.Text <> "" Then
			cmdFile.FileName = txtName.Text
			cmdFile.SaveFile(Server.MapPath("../Uploads/content/"))
			imgViewer.ImageSrc = "../Uploads/content/"+txtName.Text
			'imgViewer.Width = "300px"
			
		End If
		
		
		sSQL = "SELECT * FROM content Where id=" & recordID	
		
		
		Set rs = DBLayer.GetRecordSet(sSQL)		
		
		If rs.RecordCount = 0 Then
			rs.AddNew		
			mode = "new"
			rs("createtime").Value = Now()
			rs("usercreate").Value = CurrentUser.UserID()
		End If		
		
		rs("title").Value	= txtTitle.Text
		rs("description").Value    = txtDescription.Text
		rs("body").Value    = Request.Form("txtBody")		
		If txtStartpublish.Text<>"" Then
			rs("startpublish").Value		= txtStartpublish.Text
		Else
			rs("startpublish").Value = year(now()) & "-" & month(now()) & "-" & day(now()) & " 00:00:00"
		End If
		If txtEndpublish.Text<>"" Then
			rs("endpublish").Value		= txtEndpublish.Text	
		else
			rs("endpublish").Value = "2099-12-30 00:00:00"
		End If
				
		rs("updatetime").Value = Now()
		rs("status").Value = 1		
		rs("categoryid").Value = cboCategory.Value		
		rs("userupdate").Value = CurrentUser.UserID()	
		rs("isdeleted").Value = 0
		rs("hot").Value = chkHot.Checked
		rs("allowcomment").Value = chkAllowComment.Checked
		' update content table
		DBLayer.UpdateRecordSet rs
		
		'Get Content ID
		if mode = "new" then
			sSQL = "SELECT max(id) as id FROM content"			
			Set rs = DBLayer.GetRecordSet(sSQL)	
			recordID = rs("id").Value	
		end if
		'Response.Write("id inserted " & recordID)
		
		'Begin update File table
		sSQL = "select * from [file] where contentid = " & recordID
		Set rs = DBLayer.GetRecordSet(sSQL)	
		'Response.Write(sSQL)
		If rs.RecordCount = 0 Then
			rs.AddNew				
		End If	
		rs("contentid").Value = recordID
		rs("url").Value = "../Uploads/content/"+txtName.Text
		rs("filetype").Value = ""
		rs("filename").Value = txtName.Text
		rs("type").Value =1
		rs("isdeleted").Value = 0
		DBLayer.UpdateRecordSet rs
		'End insert File table
	End function
	Public Function cmdSave_OnClick()
		
		SaveData
		
		If mValidation.IsValid Then
			Response.Redirect "ContentEdit.asp?id=" & recordID
		End If
		
	End Function
	Public Function cmdSaveAddNew_OnClick()
		
		SaveData
		
		If mValidation.IsValid Then
			Response.Redirect "ContentEdit.asp"
		End If
		
	End Function
	Public Function Validate()
		
		'You could use several approaches to validation. I normaly use this approach and modify the control output as appropriate
	
		'(object,strFriendlyName, intDataType, bolRequired, strDefaultValue)
		mValidation.Validate 	txtTitle,"Title", VALIDATION_DATATYPE_STRING, True , ""
		mValidation.Validate 	cboCategory,"Category", VALIDATION_DATATYPE_STRING, True , ""
		
		
	End Function

	Public Function LoadRecord()
		Dim sSQL
		Dim rs
		
		cboCategory.Bind GetRecordSet("SELECT id, name, level FROM category where level >0 AND isdeleted=0" ),"id","name","",True
		
		sSQL = "SELECT title, description, body,hot,allowcomment, categoryid, startpublish, endpublish, url,filename FROM content as c left join [file] as f on c.id = f.contentid Where c.id=" & recordID
		
		Set rs = GetRecordSet(sSQL)
		'Response.Write(sSQL)
		
		If rs.RecordCount > 0 Then
			txtTitle.Text = rs("title").Value
			txtDescription.Text  = rs("description").Value
			txtBody.Text = rs("body").Value
			strBody =  rs("body").Value
			cboCategory.Items.SetSelectedByValue rs("categoryid").Value,True
			if rs("startpublish").Value <> "" Then
			txtStartpublish.Text = rs("startpublish").Value
			end if
			if rs("endpublish").Value <> "" then
			txtEndpublish.Text = rs("endpublish").Value
			end if
			chkHot.Checked = rs("hot").Value
			chkAllowComment.Checked = rs("allowcomment").Value
			'For upload image
			txtName.Text = rs("filename").Value
			imgViewer.ImageSrc = "../uploads/content/"& rs("filename").Value
			
			
					
		End If
			
	End Function

	Public Function Panel_SetVisibility(e) 
		Panel.Control.Visible = Not Panel.Control.Visible
	End Function
	'for upload file
	Public Function cmdFile_OnUpload()
		imgViewer.ImageSrc = "../Uploads/content/" + cmdFile.TempFileName
		txtName.Text = year(now()) & month(now()) & day(now())& hour(now()) & minute(now())& second(now()) & cmdFile.FileName 
		txtBody.Text = Request.Form("txtBody")
		
		SaveData
		
		If mValidation.IsValid Then
			Response.Redirect "ContentEdit.asp?id=" & recordID
		End If
	End Function
	

%>


<%Function RenderPanel() %>
<%mValidation%>
	<table border=0 ID="Table1">
		<tr ><td>Title </td><td><%txtTitle%></td></tr>
		<tr><td>Description</td><td><%txtDescription%></td></tr>
		<tr><td>Category</td><td><%cboCategory%></td></tr>
		<tr><td>Hot</td><td><%chkHot%></td></tr>
		<tr><td>Allow comment</td><td><%chkAllowComment%></td></tr>
		
		
		<tr ><td>Body</td><td>
		<% 
		         
		   txtBody.Draw()
	
		   %></td></tr>		
		<!--<tr><td colspan="2"><%'Response.Write(strBody)%></td></tr>-->
		<tr ><td>Start Publish</td><td><%txtStartpublish%></td></tr>
		<tr><td>End Publish</td><td><%txtEndpublish%></td></tr>
		<tr><td>Image</td><td><%txtName%>&nbsp;<font size="2"><%cmdFile%></font></td></tr>
		<tr><td>&nbsp;</td><td align="left"><%imgViewer%></td></tr>
		
	
		<tr><td colspan=2 align=left><a href="Content.asp"><HR>Back</a> | <%cmdSave%> | <%cmdSaveAddNew%></td></tr>
	</table>
	<br/>
	<br/>
<%End Function%>
